<?php

/* 通话记录 */
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\facade\Db;

class AdController extends AdminBaseController
{
        
    public function index()
    {
        $data = $this->request->param();
        //print_r($data);exit;
        $map=[];
        $is_show=isset($data['is_show']) ? $data['is_show']: '';
        if($is_show!=''){
            $map[]=['is_show','=',$is_show];
        }
		
		
		$keyword=isset($data['keyword']) ? $data['keyword']: '';
        if($keyword!=''){
            $map[]=['name','like','%'.$keyword.'%'];
        }
        $nums=Db::name("ad")->where($map)->count();
        $list = Db::name('ad')
            ->where($map)
            ->order('is_show asc')
            ->order('id asc')
            ->paginate(20);
        $list->each(function($v, $k){
            $v['img']=get_upload_path($v['img']);
            return $v;
        });
        $list->appends($data);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
		$this->assign('nums', $nums);
        // $this->assign('coin_status', $coin_status);
        // $this->assign('consumption_status', $consumption_status);
        // $this->assign('votes_status', $votes_status);
        // $this->assign('votestotal_status', $votestotal_status);
        // $this->assign('createtime_status', $createtime_status);
        // 渲染模板输出
        return $this->fetch();
    }
    
    public function add(){
        return $this->fetch();
    }
    
    public function addPost(){

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $img=$data['img'];
            
            if($img==''){
                $this->error("请上传图片");
            }
            


            $arr=array(
                'name'=>$data['name'],
                'desc'=>$data['desc'],
                'link'=>$data['link'],
                'is_show'=>$data['is_show'],
                'img'=>$img,
            );
            
            $id = DB::name('ad')->insertGetId($arr);
            if(!$id){
                $this->error("添加失败！");
            }
            $this->success("添加成功！");
        }
    }
    public function edit(){

        $id   = $this->request->param('id', 0, 'intval');
        
        $data=Db::name('ad')
            ->where("id={$id}")
            ->find();
        if(!$data){
            $this->error("信息错误");
        }

        //print_r($data);exit;
        $this->assign('data', $data);
        return $this->fetch();
    }
    
    public function editPost(){

        if ($this->request->isPost()) {
            $data = $this->request->param();
            $id=$data['id'];
            $img=$data['img'];
            
            if($img==''){
                $this->error("请上传图片");
            }
            

            $arr=array(
                'id'=>$id,
                'name'=>$data['name'],
                'desc'=>$data['desc'],
                'link'=>$data['link'],
                'is_show'=>$data['is_show'],
                'img'=>$img,
            );
            //print_r($arr);exit;
            $rs = DB::name('ad')->update($arr);

            if($rs === false){
                $this->error("保存失败！");
            }
            $this->success("保存成功！");
        }
    }
    
    public function del(){

        $id = $this->request->param('id', 0, 'intval');
        //var_dump($id);exit;
        $rs = DB::name('ad')->where("id={$id}")->delete();
        
        if(!$rs){
            $this->error("删除失败！");
        }
        $this->success("删除成功！",url("admin/Ad/index"));
    }

}